﻿@helper Branch(int folderID)
{
    var folders = (Model.Folders.Items as IEnumerable<dynamic>).Where(x => folderID != 0
        ? (x.Parent != null && JDM.Int32(x.Parent.EntityID) == folderID) 
        : x.Parent == null);
    var files = (Model.Files.Items as IEnumerable<dynamic>).Where(x => folderID != 0
        ? (x.Folder != null && JDM.Int32(x.Folder.EntityID) == folderID)
        : x.Folder == null);
    <li class="unit file-folder">
    @{
        if(files.Any())
        {
            Html.RenderPartial("_Downloads", files);
        }
        foreach(var i in folders)
        {
            var t_folder = (i.Translations as IEnumerable<dynamic>)
                .SingleOrDefault(x => JDM.Int32(x.Language.EntityID) ==
                    JDM.Int32(Svc.Localization.Language.EntityID));
            <label>&rArr;&nbsp;@String.Format("{0} ({1}|{2})", 
                t_folder != null 
                    ? JDM.String(t_folder.Name) 
                    : JDM.String(i.EntityID),
                JDM.Int32(i.FileCount),
                Svc.DataSize((decimal)JDM.Decimal(i.Size)))</label>
            <ul class="unit-list file-folder">
                @Branch(JDM.Int32(i.EntityID))
            </ul>
        }
    }
    </li>
}

<ul class="unit-list file-folder">
@if((Model.Files.Items as IEnumerable<dynamic>).Any())
{
    @Branch(0)
}
else
{
    <li class="empty"><span>@T(Res.In("Common").Of("Expr").Name("NoData"))</span></li>
}
</ul>